//
//  FunImageBgView.m
//  markmap
//
//  Created by 甘世清 on 2024/8/14.
//

#import "FunImageBgView.h"
#import "FunUICollectionViewFlowLayout.h"
@implementation FunImageBgView

- (void)SQ_UIViewForSelfView{
    [self.collectionView registerClass:[AddFunyPicCollectionViewCell class] forCellWithReuseIdentifier:@"AddFunyPicCollectionViewCell"];
    [self addSubview:self.collectionView];
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self);
    }];
   
}
- (void)setImageArray:(NSMutableArray *)imageArray{
    _imageArray = imageArray;
    if (self.imageArray.count == 1) {
     //1张 处理url
        WS(weakSelf);
        NSString * urlstring = self.imageArray.firstObject;
        NSArray * sizeArray = [urlstring componentsSeparatedByString:@"&"];
        if (sizeArray.count >2) {
            NSString * imageWidth = [NSString stringWithFormat:@"%@",[sizeArray objectAtIndex:1]];
            NSString * imageHieght = [NSString stringWithFormat:@"%@",[sizeArray objectAtIndex:2]];
            float width = [imageWidth floatValue];
            float height = [imageHieght floatValue];
            if (width < height) {
               if (height > SCREEN_WIDETH-50) {
                   height = SCREEN_WIDETH-50;
                   width = height/[imageHieght floatValue] * [imageWidth floatValue];
               }
           } else {
               if (width > SCREEN_WIDETH-50) {
                   width = SCREEN_WIDETH-50;
                   height = width/[imageWidth floatValue] * [imageHieght floatValue];
               }
           }
            self.cellSize = CGSizeMake(width,height+10);
        } else {
            self.cellSize = CGSizeMake(SCREEN_WIDETH-50, SCREEN_WIDETH-30);
        }
      
        [weakSelf.collectionView reloadData];
        [weakSelf mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(weakSelf.cellSize.height+10);
        }];
    }else if (self.imageArray.count ==2) {
        [self mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(SCREEN_WIDETH/2.f);
        }];
    }else if (self.imageArray.count ==3) {
        [self mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(SCREEN_WIDETH/3.f);
        }];
    } else if (self.imageArray.count ==4){
        [self mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(((SCREEN_WIDETH-34)/2.f-20)*2);
        }];
    } else if (self.imageArray.count ==5||self.imageArray.count ==6){
        [self mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(SCREEN_WIDETH/3.f*2+10);
        }];
    }else{
        [self mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo((SCREEN_WIDETH));
        }];
    }
    [self.collectionView reloadData];
}

#pragma mark UICollectionViewDelegate,UICollectionViewDataSource
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.imageArray.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView*)collectionView cellForItemAtIndexPath:(nonnull NSIndexPath *)indexPath{
    AddFunyPicCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"AddFunyPicCollectionViewCell" forIndexPath:indexPath];
    cell.typeBtn.hidden =  YES;
    if (self.imageArray.count ==1) {
        NSString * urlstring = self.imageArray.firstObject;
        NSArray * sizeArray = [urlstring componentsSeparatedByString:@"&"];
        [cell.bgImage sd_setImageWithURL:SQURLImage(sizeArray.firstObject)];
    } else {
        [cell.bgImage sd_setImageWithURL:SQURLImage([self.imageArray objectAtIndex:indexPath.item])];
    }
    cell.contentView.layer.cornerRadius = 8;
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (self.imageArray.count ==1) {
     //1张
        return self.cellSize;
    } else  if (self.imageArray.count ==2) {
      //4张
        return CGSizeMake((SCREEN_WIDETH-34)/2.f-1, (SCREEN_WIDETH-34)/2.f-1); ;
    }else  if (self.imageArray.count ==4) {
        //4张
          return CGSizeMake((SCREEN_WIDETH-34)/2.f-30, (SCREEN_WIDETH-34)/2.f-30); ;
      }
    //n张
    return CGSizeMake((SCREEN_WIDETH-44)/3.f-1, (SCREEN_WIDETH-44)/3.f-1);
}

 
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
        if (self.isDetaile) {
            NSMutableArray * dataArray = [NSMutableArray new];
             if (self.imageArray.count ==1) {
                NSString * urlstring = self.imageArray.firstObject;
                NSArray * sizeArray = [urlstring componentsSeparatedByString:@"&"];
                 YBIBImageData * data = [YBIBImageData new];
                 data.imageURL = SQURLImage(sizeArray.firstObject);
                 data.projectiveView = [self.collectionView cellForItemAtIndexPath:indexPath];
                 [dataArray addObject:data];
                } else {
                    for (int x = 0; x<self.imageArray.count; x++) {
                        YBIBImageData * data = [YBIBImageData new];
                        data.imageURL = SQURLImage([self.imageArray objectAtIndex:x]);
                         data.projectiveView = [self.collectionView cellForItemAtIndexPath:indexPath];
                        [dataArray addObject:data];
                 }
            }
            YBImageBrowser * brow = [YBImageBrowser new];
            brow.dataSourceArray = dataArray;
            brow.currentPage =  indexPath.item;
            [brow show];
        } else {
            FunyPicDetailsViewController * vc = [[FunyPicDetailsViewController alloc]init];
            vc.model = self.model;
            [[Manager SQ_getCurrentViewController].navigationController pushViewController:vc animated:YES];
        }
}

- (UICollectionView *)collectionView
{
    if (!_collectionView) {
        FunUICollectionViewFlowLayout *layout = [[FunUICollectionViewFlowLayout alloc] init];
          layout.minimumLineSpacing = 10;
          layout.minimumInteritemSpacing = 10;
          layout.sectionInset = UIEdgeInsetsMake(10, 10 ,0, 10);
          layout.scrollDirection = UICollectionViewScrollDirectionVertical;
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
        _collectionView.showsVerticalScrollIndicator = NO;
        _collectionView.showsHorizontalScrollIndicator = NO;
        _collectionView.userInteractionEnabled = YES;
        _collectionView.backgroundColor = [UIColor clearColor];
        _collectionView.alwaysBounceVertical = YES;
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
       
    }
    return _collectionView;
}
@end
